package com.lizhe.jdbc.dao;

import com.lizhe.jdbc.modle.Article;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * 类描述:
 * 持久层dao
 *
 * @author Administrator
 * @create 2021-10-26 21:35
 */

// @Repository 持久层依赖注入注解
@Repository
public class ArticleJDBCDAO {

    // 单个数据源直接引入 JdbcTemplate
    // @Resource 多个数据源测试的时候这个需要先注释掉
    private JdbcTemplate jdbcTemplate;



    public void save(Article article) {
        jdbcTemplate.update("INSERT INTO article(author, title,content,create_time) values(?, ?, ?, ?)",
                article.getAuthor(),
                article.getTitle(),
                article.getContent(),
                article.getCreateTime());
    }

    //删除文章
    public void deleteById(Long id) {
        //jdbcTemplate.update适合于insert 、update和delete操作；
        jdbcTemplate.update("DELETE FROM article WHERE id = ?", id);

    }


    //更新文章
    public void updateById(Article article) {
        //jdbcTemplate.update适合于insert 、update和delete操作；
        jdbcTemplate.update("UPDATE article SET author = ?, title = ? ,content = ?,create_time = ? WHERE id = ?",
                article.getAuthor(),
                article.getTitle(),
                article.getContent(),
                article.getCreateTime(),
                article.getId());

    }


    //根据id查找文章
    public Article findById(Long id) {
        //queryForObject用于查询单条记录返回结果
        return (Article) jdbcTemplate.queryForObject("SELECT * FROM article WHERE id=?",
                new Object[]{id}, new BeanPropertyRowMapper<>(Article.class));
    }


    //查询所有
    public List<Article> findAll() {
        //query用于查询结果列表
        return (List<Article>) jdbcTemplate.query("SELECT * FROM article ", new BeanPropertyRowMapper<>(Article.class));
    }


}